#include "mainwindow.h"
#include <QApplication>
#include <QObject>
#include <QTextCodec>
#include <QThread>
#include <QMetaType>
#include <QFontDatabase>

/**
 * @brief main
 *
 *
 */

int main(int argc, char *argv[])
{
    //QApplication::setStyle("cleanlooks");//原本hmi的设置
    QApplication a(argc, argv);

    qRegisterMetaType<QVector<QString> > ("QVector<QString>");
    qRegisterMetaType<QVector<int> > ("QVector<int>");
    qRegisterMetaType<ComponentVersion>("ComponentVersion");
    qRegisterMetaType<CommRobotInfo>("CommRobotInfo");
    qRegisterMetaType<MoveParameter>("MoveParameter");
    qRegisterMetaType<CurrentRunFile>("CurrentRunFile");
    qRegisterMetaType<FileInfo>("FileInfo");
    qRegisterMetaType<PointPro>("PointPro");
    qRegisterMetaType<Message>("Message");
    qRegisterMetaType<PlRegStruct>("PlRegStruct");
    qRegisterMetaType<AxisLimit>("AxisLimit");
    qRegisterMetaType<HmiToolCoordinateInformation>("HmiToolCoordinateInformation");
    qRegisterMetaType<HmiUserCoordinateInformation>("HmiUserCoordinateInformation");
    qRegisterMetaType<QVector<HmiToolCoordinateInformation> >("QVector<HmiToolCoordinateInformation>");
    qRegisterMetaType<QVector<HmiUserCoordinateInformation> >("QVector<HmiUserCoordinateInformation>");
    qRegisterMetaType<ReturnRegisterStatus>("ReturnRegisterStatus");
    qRegisterMetaType<ControllerIpGroupInfo>("ControllerIpGroupInfo");

    //**********主界面,风格**********
    MainWindow mainWindow;

    //**********编码,字体设定**********
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
    QTextCodec *codec = QTextCodec::codecForName( "UTF-8" );
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);
#else
    QTextCodec *codec = QTextCodec::codecForName( "UTF-8" );
    QTextCodec::setCodecForLocale(codec);
#endif



    //**********global数据初始化**********
    GlobalData globalData;
    GlobalData ioData;

    //**********gsoap通信初始化**********
    //update lv2定时
    CommunicateUpdateDelegate *communicateThread = new CommunicateUpdateDelegate;
    globalData.ioUpdateFlag = 2;                //LV2
    communicateThread->hmiCommunicate->globalData = &globalData;

    //update lv3 定时
    CommunicateUpdateDelegate *communicateThreadLV3 = new CommunicateUpdateDelegate;
    ioData.ioUpdateFlag = 3;                    //默认已经是0 通过mid中修改标志位获取lv3. hualei  =0时，数据不更新，＝２时，数据ＬＶ２更新，＝３时　ＬＶ３更新
    communicateThreadLV3->hmiCommunicate->globalData = &ioData;

    //普通操作
    CommunicateNormalDelegate *communicateNormalThread = new CommunicateNormalDelegate;
    communicateNormalThread->hmiCommunicate->globalData = &globalData;

    //安全操作
    CommunicateSaveDelegate *communicateSaveThread = new CommunicateSaveDelegate;
    communicateSaveThread->hmiCommunicate->globalData = &globalData;

    //**********运动操作初始化*********
    MoveOperation moveOperation;
    moveOperation.globalData = &globalData;
    //moveOperation.ioData = &ioData;//暂时用不上
    QObject::connect(&moveOperation,SIGNAL(moveJogSignal(int,int)),communicateNormalThread,SLOT(jogRunSlot(int,int)));
    QObject::connect(&moveOperation,SIGNAL(movePointSignal(MoveParameter)),communicateNormalThread,SLOT(moveToPointSlot(MoveParameter)));
    QObject::connect(&moveOperation,SIGNAL(stopRunSignal()),communicateSaveThread,SLOT(stopRun()));
    QObject::connect(&moveOperation,SIGNAL(runningDog()),communicateNormalThread,SLOT(jogRunDogSlot()));

    mainWindow.moveOperation=&moveOperation;





    //**********login界面初始化**********
    Login *loginView = new Login;
    Delegate *delegate = new Delegate;
    delegate->loginView = loginView;
    delegate->loginStatus = loginView->loginStatus;
    delegate->ipSetDialog = loginView->ipSetDialog;
    loginView->loginStatus->globalData = &globalData;//主要用于控制ip搜寻
    delegate->globalData = &globalData;
    delegate->ioData = &ioData;
    delegate->mountDelegate->globalData = &globalData;
    delegate->initConnect();
    delegate->readIniCtrlIP();
    /* delegate 读取ini文件 获取ip 设置系统网络 设置语言显示(获取了所有的loginView变量)
     * 并且接下来的loginView->updateShow();会根据变量刷新界面
     */
    //连接delegate与通信处理:更新用户信息用;尝试连接;
    QObject::connect(delegate,SIGNAL(callTmpInfoUpdate(QString)),communicateNormalThread,SLOT(updateTmpInfo(QString)));
    QObject::connect(delegate,SIGNAL(callTmpInfoUpdate(QString)),communicateSaveThread,SLOT(updateTmpInfo(QString)));
    QObject::connect(delegate,SIGNAL(callTmpInfoUpdate(QString)),communicateThread,SLOT(updateTmpInfo(QString)));
    QObject::connect(delegate,SIGNAL(callTmpInfoUpdate(QString)),communicateThreadLV3,SLOT(updateTmpInfo(QString)));

    QObject::connect(delegate,SIGNAL(tryToConnect()),communicateNormalThread,SLOT(tryToConnect()));
    QObject::connect(communicateNormalThread,SIGNAL(sendConnectResult(int,QString)),delegate,SLOT(recvConnectResult(int,QString)));

    QObject::connect(delegate,SIGNAL(tryToFindIP()),communicateNormalThread,SLOT(findConnect()));
    QObject::connect(delegate,SIGNAL(stopFindIP()),communicateNormalThread,SLOT(stopFindConnect()));
    QObject::connect(communicateNormalThread,SIGNAL(sendFindConnectResult(QVector<QString>)),delegate,SLOT(findIpResultSlot(QVector<QString>)));
	//语言
    QObject::connect(delegate,SIGNAL(setLanguageSignal(int)),communicateNormalThread,SLOT(setLanguageSlot(int)));
    //获取并且打开缺省文件
    QObject::connect(delegate->mountDelegate,SIGNAL(readDefaultFileNameSignal()),communicateNormalThread,SLOT(readDefaultFileNameSlot()));
    ////login 控制 delegate和mainwindow的load;手动切换(下拉选择语言)的时候就直接触发
    QObject::connect(loginView, SIGNAL(controllerLanguageChangedSignal(int)), delegate, SLOT(changeLanguageSlot(int)));
    QObject::connect(loginView, SIGNAL(controllerLanguageChangedSignal(int)), &mainWindow, SLOT(changeLanguageSlot(int)));

    //update中会把子页面的语言也刷新
    loginView->updateShow();//建立语言槽后,updateShow中会控制delegate和mainWindow

    qDebug()<<"login init";





    //**********gsoap通信:移交线程与信号槽关联**********
    //LV2 LV-MSG
    QThread updateThread;                                   //
    communicateThread->moveToThread(&updateThread);
    updateThread.start();
    communicateThread->timerOrder->start(GSOAP_UPDATE);//外部启动

    //LV3
    QThread updateLV3Thread;                                //
    communicateThreadLV3->moveToThread(&updateLV3Thread);
    updateLV3Thread.start();
    communicateThreadLV3->timerOrder->start(GSOAP_UPDATE_400);

    //普通
    QThread normalThread;                                   //非指针类型可行,没有内存泄露情况
    communicateNormalThread->moveToThread(&normalThread);
    normalThread.start();

    //安全
    QThread saveThread;                                     //非指针类型可行,没有内存泄露情况
    //QObject::connect(&saveThread, SIGNAL(finished()), communicateSaveThread, SLOT(deleteLater()));
    communicateSaveThread->moveToThread(&saveThread);
    saveThread.start();
    qDebug()<<"4 thread start";




    //setParent 按先后顺序 登录页面显示于最上层,登陆完成后hide
    loginView->setParent(&mainWindow);
    loginView->setStyleSheet("background-color:white;outline: none;");
    loginView->show();

//    //补充 隔膜层 用于topView的退出
//    HmiFilmLabel *film = new HmiFilmLabel;
//    film->setParent(&mainWindow);
//    film->hide();
//    mainWindow.film = film;
    //补充 data和线程 用于mainWindow控制
    mainWindow.globalData = &globalData;
    mainWindow.ioData = &ioData;
//    mainWindow.lv2Thread = &communicateThread;
//    mainWindow.lv3Thread = &communicateThreadLV3;
//    mainWindow.normalThread = &normalThread;
//    mainWindow.saveThread = &saveThread;


    mainWindow.communicateNormalThread=communicateNormalThread;
    mainWindow.communicateSaveThread=communicateSaveThread;
    mainWindow.show();
    qDebug()<<"mainWindow show";//

    return a.exec();
}
